﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;
using CTEM.Controllers.BUS;
using CTEM.Models;
using CTEM.Controllers.Global;
using System.Diagnostics;
using System.Data.SqlClient;
using DevExpress.XtraGrid.Views.Grid;

namespace CTEM.Controllers.Global
{
    class Global
    {
        public static List<RightDTO> Rights;
        public static EmployerDTO CurrentUser;
        public static string SEP = Constants.SPLIP_MONEY;
        public static List<string> ListError = new List<string>();

        public static void InitLayoutGridView(GridView gridView)
        {
            gridView.OptionsView.NewItemRowPosition = NewItemRowPosition.Top;
            gridView.OptionsView.ColumnAutoWidth = false;
            gridView.OptionsView.ShowAutoFilterRow = true;
            gridView.OptionsView.ShowFooter = true;
        }

        public static string LogMessage(string filename, string functionname, string message)
        {
            return String.Format("File Name: {0} - Function Name: {1} - Message: {2}", filename, functionname, message);
        }

        public static SqlParameter[] SetSqlParameter(string[] parameternames, object[] values)
        {
            int numberParameters = parameternames.Length;
            SqlParameter[] SqlParameters = new SqlParameter[numberParameters];
            for (int i = 0; i < parameternames.Length; i++)
            {
                SqlParameters[i] = new SqlParameter(parameternames[i], values[i]);
            }
            return SqlParameters;
        }

        public static string TitleCase(string s)
        {
            s = s.Trim();
            s = s.ToLower();
            s = Regex.Replace(s, "\\b[\\w]\\w*", match =>
                {
                    string c = match.ToString();
                    return char.ToUpper(c[0]) + c.Substring(1);
                });
            return s;
        }

        public static string RemoveRedundantSpace(string s)
        {
            s = s.Trim();
            var rg = new Regex(@"(\s+)");
            s = rg.Replace(s, " ");
            return s;
        }

        public static string RemoveSpace(string s)
        {
            s = s.Trim();
            var rg = new Regex(@"(\s+)");
            return rg.Replace(s, Constants.EMPTY_TEXT);
        }

        public static bool IsAllow(string right)
        {
            long rightValue = 0;
            foreach (RightDTO rightDTO in Global.Rights)
            {
                if (right.Equals(rightDTO.RightName))
                {
                    rightValue = rightDTO.Value;
                }
            }
            return (rightValue & CurrentUser.RightsValue) != 0;
        }

        public static long ConvertMoneyToLong(string strValue, string charFomat)
        {
            try
            {
                return Convert.ToInt64(strValue.Trim().Replace(charFomat, Constants.EMPTY_TEXT));
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static double ConvertMoneyToDouble(string strValue, string charFomat)
        {
            try
            {
                strValue = strValue.Trim().Replace(charFomat, Constants.EMPTY_TEXT);
                strValue = strValue.Trim().Replace(",", ".");
                return Convert.ToDouble(strValue);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static string ConvertLongToMoney(long number, string charFomat)
        {
            string result = Constants.EMPTY_TEXT;
            string strNumber = number.ToString();
            int sizeStr = strNumber.Length;
            for (int i = 0; i < sizeStr; i++)
            {
                result += strNumber[i];
                int tempSize = sizeStr - i - 1;
                float a = (sizeStr - i - 1) % 3;
                if (tempSize != 0 && a == 0)
                {
                    result += Constants.SPLIP_MONEY;                    
                }
            }            
            return result;
        }

        public static string ConvertDoubleToMoney(double number, string charFomat)
        {
            string result = Constants.EMPTY_TEXT;
            string strNumber = number.ToString();
            char[] c = {'.'};
            string[] dd = strNumber.Split(c, 2);
            string hearder = dd[0];
            string fofter = dd.Length == 2 ? ("," + dd[1]) : "";
            int sizeStr = hearder.Length;
            for (int i = 0; i < sizeStr; i++)
            {
                result += hearder[i];
                int tempSize = sizeStr - i - 1;
                float a = (sizeStr - i - 1) % 3;
                if (tempSize != 0 && a == 0)
                {
                    result += Constants.SPLIP_MONEY;
                }
            }
            result += fofter;
            return result;
        }

        public static List<RoleDTO> SetCompleteRole(List<RoleDTO> listItem, string keySearch)
        {
            var listDes = new List<RoleDTO>();
            foreach (var item in listItem)
            {
                if (item.RoleName.Contains(keySearch))
                {
                    listDes.Add(item);
                }
            }
            return listDes;
        }

        public static double Percentage(double numerator, double denominator)
        {
            double result = (numerator/denominator)*100;
            result = Convert.ToDouble(result.ToString("0.00"));
            return result;
        }

        public static DateTime GetFirstDateInMonth()
        {            
            string month = DateTime.Today.Month.ToString();
            if (month.Length == 1)
            {
                month = "0" + month;
            }
            string year = DateTime.Today.Year.ToString();
            string firstDateInMonthStr = String.Format("{0}/01/{1} 00:00:00", month, year);
            return DateTime.ParseExact(firstDateInMonthStr, "MM/dd/yyyy hh:mm:ss", null);
        }

        public static DateTime GetLastDateInMonth()
        {            
            int month = DateTime.Today.Month;
            string strmonth = month.ToString();
            if (strmonth.Length < 1)
            {
                strmonth = "0" + strmonth;
            }
            int year = DateTime.Today.Year;
            int date = DateTime.DaysInMonth(year, month);
            string firstDateInMonthStr = String.Format("{0}{1}{2} 00:00:00", strmonth, date, year);
            return DateTime.ParseExact(firstDateInMonthStr, "MM/dd/yyyy hh:mm:ss", null);
        }

        public static string FomatPhoneNumber(string phonenumber)
        {
            try
            {
                phonenumber = phonenumber.Replace("-", "");
                phonenumber = phonenumber.Replace("(", "");
                phonenumber = phonenumber.Replace(")", "");
                string header = String.Format("({0})", phonenumber.Substring(0, phonenumber.Length - 7));
                string mainNumber = phonenumber.Substring(phonenumber.Length - 7);
                mainNumber = String.Format("{0}-{1}-{2}", mainNumber[0], mainNumber.Substring(1, 3), mainNumber.Substring(4));
                string result = header + mainNumber;
                return result;
            }
            catch
            {                
            }
            return phonenumber;
        }

        public static bool ValidateDateFromTo(DateTime dateTimeFrom, DateTime dateTimeTo)
        {
            if (dateTimeFrom > dateTimeTo)
            {
                ListError.Add(Constants.INVALID_DATE);
                return false;
            }
            return true;
        }

        public static bool ValidateInputNumber(string strNumber)
        {
            for (int i = 0; i < strNumber.Length; i++)
            {
                if (strNumber[i] < '0' || strNumber[i] > '9')
                {
                    return false;
                }
            }
            return true;
        }

        public static bool GenerateStatus(string strStatus)
        {
            bool isActie = strStatus.Equals(Constants.PAY);
            return isActie;
        }
    }
}
